package com.haven.webrobotserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haven.webrobotserver.domain.UserKeyword;
import com.haven.webrobotserver.service.UserKeywordService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/UserKeyword")
@CrossOrigin(origins = "http://localhost:8080")
public class UserKeywordController {

    @Autowired
    private UserKeywordService userKeywordService;

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

    @PostMapping("/getList")
    public JSONObject getUserKeywordList(@RequestBody Map<String,String> params){
        JSONObject result = new JSONObject();

        /*prepare params*/
        String name = params.get("name");
        String striProjectId = params.get("projectId");
        int projectId ;
        if(striProjectId.equals("")){
            projectId = -1;
        }else{
            projectId=Integer.parseInt(striProjectId);
        }
        String description = params.get("description");


        /*getlist*/
        List<UserKeyword> uks = userKeywordService.getList(name, projectId, description);

        String s = JSON.toJSONString(uks);
        JSONArray ukJsons = JSON.parseArray(s);

        if(ukJsons != null){
            result.put("data",ukJsons);
            result.put("status",200);
            result.put("msg","获取组件列表成功");
            return result;
        }

        result.put("status",400);
        result.put("msg","获取组件列表失败");
        return result;
    }

    @PostMapping("/update")
    public JSONObject editUserKeyword(@RequestBody Map<String,String> params){
        JSONObject result = new JSONObject();

        /*封装userkeyyword，交给ukservice*/
        UserKeyword userKeyword = new UserKeyword();

        try{
            userKeyword.setName(params.get("name"));
            userKeyword.setCode(params.get("code"));
            userKeyword.setProjectId(Integer.parseInt(params.get("projectId")));
            userKeyword.setDescription(params.get("description"));


            String id = params.get("id");
            if(id != null && !id.equals("")){
                userKeyword.setId(Integer.parseInt(id));

                userKeywordService.updateUK(userKeyword);

            }else{
                userKeyword.setId(-1);
                Date date = new Date();
                userKeyword.setCreatedate(date);

                userKeywordService.insertNewUK(userKeyword);
            }

            result.put("status",200);
            result.put("data","");
            result.put("msg","组件保存成功");

        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("data","");
            result.put("msg","组件保存失败");
        }



        return result;
    }

    @GetMapping("/deleteOne")
    public JSONObject deleteOne(@RequestParam(name = "id") int id){
        JSONObject result = new JSONObject();
        try{
            userKeywordService.deleteOne(id);
            result.put("status",200);
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
        }

        result.put("data","");
        return result;
    }
}
